package com.gene.controller;

import com.gene.pojo.UserCollection;
import com.gene.pojo.entity.Favorite;
import com.gene.service.IUserCollectionService;
import com.gene.utils.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 用户收藏表
 */
@Controller
@RequestMapping("/gene-safe/userCollection")
public class UserCollectionController {
    @Autowired
    private IUserCollectionService userCollectionService;

    /**
     * 查询    userId为查询条件
     * @param userCollection
     * @return
     */
    @RequestMapping(value="/query")
    @ResponseBody
    public ResultData query(UserCollection userCollection){
        ResultData result=new ResultData();
        List<Favorite> list=userCollectionService.query(userCollection);

        if(list.size()>0){
            result.setData(list);
            result.setStatus("1");
            result.setMsg("查询成功！");
        }else{
            result.setStatus("0");
            result.setMsg("没有数据！");
        }

        return result;
    }



    /**
     * 查询    collectionId为查询条件
     * @param userCollection
     * @return
     */
    @RequestMapping(value="/selectId")
    @ResponseBody
    public ResultData selectId(UserCollection userCollection){
        ResultData result=new ResultData();
        Favorite favorite = userCollectionService.selectId(userCollection);

        if(null != favorite){
            result.setData(favorite);
            result.setStatus("1");
            result.setMsg("查询成功！");
        }else{
            result.setStatus("0");
            result.setMsg("没有数据！");
        }

        return result;
    }


    /**
     * 新增
     * @param userCollection
     * @return
     */
    @RequestMapping(value="/add")
    @ResponseBody
    public ResultData add(UserCollection userCollection){
        ResultData result =new ResultData();
        boolean a=userCollectionService.add(userCollection);

        if(a){
            result.setStatus("1");
            result.setMsg("新增成功...");
        }else{
            result.setStatus("0");
            result.setMsg("新增失败！");
        }
        return result;
    }

    /**
     * 修改
     *
     * @param userCollection
     * @return
     */
    @RequestMapping(value="/edit")
    @ResponseBody
    public ResultData edit(UserCollection userCollection){
        ResultData result =new ResultData();
        boolean a=userCollectionService.edit(userCollection);
        if(a){
            result.setStatus("1");
            result.setMsg("修改成功...");
        }else{
            result.setStatus("0");
            result.setMsg("修改失败！");
        }
        return result;
    }

    /**
     * 删除
     * @param userCollection
     * @return
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    public ResultData delete(UserCollection userCollection) {
        ResultData result = new ResultData();
        boolean a=userCollectionService.delete(userCollection);
        if(a){
            result.setStatus("1");
            result.setMsg("删除成功...");
        }else{
            result.setStatus("0");
            result.setMsg("删除失败！");
        }
        return result;
    }




    /**
     * 传userId   itemId  判断该商品是不是被收藏
     *
     * @param userCollection
     * @return
     */
    @RequestMapping(value="/enshrine")
    @ResponseBody
    public ResultData enshrine(UserCollection userCollection){
        ResultData result =new ResultData();
        boolean a=userCollectionService.enshrine(userCollection);
        if(a){
            result.setStatus("1");
            result.setMsg("已经加入收藏夹...");
        }else{
            result.setStatus("0");
            result.setMsg("没有加入收藏夹！");
        }
        return result;
    }

}
